package com.yzd.crm.web.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.yzd.crm.domain.BaseDict;
import com.yzd.crm.domain.Customer;
import com.yzd.crm.service.CustomerService;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/4/3 14:31
 * @Description:
 */
@Controller("customerAction")
@Scope("prototype")
public class CustomerAction implements ModelDriven<Customer> {
    private Customer customer = new Customer();
    private String msg;
    @Autowired
    private CustomerService customerService;

    @Override
    public Customer getModel() {
        return customer;
    }

    public String add(){
//        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");

//        ServletContext application = ServletActionContext.getServletContext();
//        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(application);
//        CustomerService service = (CustomerService) ac.getBean(CustomerService.class);

        customerService.add(customer);
        msg = "成功";
        return "success";
    }

    List<BaseDict> levelList;
    List<BaseDict> sourceList;
    List<BaseDict> industryList;
    public List<BaseDict> getLevelList() {
        return levelList;
    }
    public List<BaseDict> getSourceList() {
        return sourceList;
    }
    public List<BaseDict> getIndustryList() {
        return industryList;
    }
    /**
     * 跳转到添加页面
     */
    public String addUi(){
        levelList = customerService.findLevel("006");
        sourceList = customerService.findSource("002");
        industryList = customerService.findIndustry("001");
        return "addUi";
    }
    /**
     * 新增客户--保存客户
     */
    public String save(){
        customerService.save(customer);
        return "customerList";
    }

    List<Customer> customerList;
    public List<Customer> getCustomerList() {
        return customerList;
    }
    /**
     * 客户列表
     */
    public String list(){
        customerList = customerService.findAll();
        return "list";
    }

    /**
     * 条件查询
     */
    public String conditionFind(){
        DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); //from Customer
        dc.add(Restrictions.like("custName", "%" + customer.getCustName() + "%"));
        customerList = customerService.conditionFind(dc);
        return "list";
    }

    private Customer customerFind;
    public Customer getCustomerFind() {
        return customerFind;
    }
    public String editUI(){
        levelList = customerService.findLevel("006");
        sourceList = customerService.findSource("002");
        industryList = customerService.findIndustry("001");
        customerFind = customerService.findById(customer.getCustId());
        return "editUI";
    }

    public String update(){
        customerService.update(customer);
        return "customerList";
    }

    public String delete(){
        customerService.delete(customer);
        return "customerList";
    }
}
